/**
 * Created by three on 14-11-6.
 */
'use strict';
(function (app) {
    app.directive("remoteValid", function () {
        return {
            require: "ngModel",
            scope: {
                remoteUrl: '@remote-valid'
            },
            link: function (scope, elm, attrs, ctrl) {
                elm.bind('keyup', function() {
                    $http({method: 'GET', url: scope.remoteUrl}).
                        success(function(data, status, headers, config) {
                            if(parseInt(data)==0){
                                ctrl.$setValidity(attrs['name'],true);
                            }else{
                                ctrl.$setValidity(attrs['name'],false);
                            }
                        }).
                        error(function(data, status, headers, config) {
                            ctrl.$setValidity('remote', false);
                        });
                });
            }
        };
    });
}(app));